<template>
  <div>
    <el-upload
      :action="uploadImgUrl"
      :data="imgTypeObj"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      ref="imageUpload"
      :on-remove="handleDelete"
      :headers="headers"
    >
    <el-button size="small" type="primary">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    <div slot="file" slot-scope="{file}">
      <image-preview :src="imageUrl" :width="50" :height="50"/>
    </div>
    </el-upload>
    
    
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

export default {
  props: {
    value: [String, Object, Array],
    // 图片数量限制
    limit: {
      type: Number,
      default: 5,
    },
    // 大小限制(MB)
    fileSize: {
      type: Number,
      default: 500,
    },
    // 文件类型, 例如['png', 'jpg', 'jpeg']
    fileType: {
      type: Array,
      default: () => ["png", "jpg", "jpeg"],
    },
    // 是否显示提示
    isShowTip: {
      type: Boolean,
      default: true
    },
    // 图片类型：avatar，product，brand
    imgType: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      imgTypeObj: null,
      imageUrl: "",
      dialogVisible: false,
      hideUpload: false,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/oss/sign/img", // 上传的图片服务器地址
      headers: {
        Authorization: "Sys- " + getToken(),
      },
    };
  },
  mounted () {
    this.imgTypeObj= {imgType: this.imgType}; //用props中的对象对data中的对象赋初始值
  },
  watch: {
    imgType:{
      // 监听数据发生变化的处理函数
      handler(newV) {
        this.imgTypeObj = {imgType: newV};
      },
      deep: true
    }
  },
  computed: {
    // 是否显示提示
    showTip() {
      return this.isShowTip && (this.fileType || this.fileSize);
    },
  },
  methods: {
    // 上传前loading加载
    handleBeforeUpload(file) {
      console.log(this.imgTypeObj);
      let isImg = false;
      if (this.fileType.length) {
        let fileExtension = "";
        if (file.name.lastIndexOf(".") > -1) {
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
        }
        isImg = this.fileType.some(type => {
          if (file.type.indexOf(type) > -1) return true;
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
          return false;
        });
      } else {
        isImg = file.type.indexOf("image") > -1;
      }

      if (!isImg) {
        this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`);
        return false;
      }
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$modal.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
      this.$modal.loading("正在上传图片，请稍候...");
    },
    // 上传成功回调
    handleUploadSuccess(res) {
      if (res.code == 200) {
        this.imageUrl = res.url;
        this.$modal.closeLoading();
        this.uploadedSuccessfully();
      } else {
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
      }
    },
    // 删除图片
    handleDelete(file) {
      console.log("删除图片====》{}",file);
    },
    // 上传失败
    handleUploadError() {
      this.$modal.msgError("上传图片失败，请重试");
      this.$modal.closeLoading();
    },
    // 上传结束处理
    uploadedSuccessfully() {
      if (this.imageUrl) {
        this.$emit("input", this.imageUrl);
        this.$modal.closeLoading();
      }
    },
  }
}
</script>
<style scoped lang="scss">

</style>

